package com.wcy.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

@Component
public class GatewayAuthFilter extends OncePerRequestFilter {

    @Value("${security.internal.token}")
    private String internalGatewayToken;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("provider::uri:"+request.getRequestURI());
        String gatewayToken = request.getHeader("X-My-Consumer-Token");
        System.out.println("启动访问");
        if(gatewayToken != null && gatewayToken.equals(internalGatewayToken)){
            filterChain.doFilter(request, response);
            return;
        }
        System.out.println("访问禁止");
        response.setStatus(403);
    }
}
